var loginUsername = "${loginUsername}"; // 获得当前登录人员的userName
var partUid = '${request.getParameter("partUid")}';
var date = new Date();
var warehouseId = "0500"; // 默认库房

// @Bind #textEmployee.onPost
!function(self, arg, dsInvIO, dsDept) {
	view.id("ajGetEmp").set("parameter", self.get("text")).execute(function(result){
		//dorado.messageBox.alert("请输入正确的编号...");
		if (result){
			var invio = dsInvIO.insert();
			// alert(curEntity.get("dept").get("deptName"));
			invio.set("ioType", 212);
			invio.set("ioTypeDesc.idDescription", '检验出库');
			invio.set("ioState", 1);
			invio.set("ioStateDesc.idDescription", '草稿');
			invio.set("ioDate", date);
		//alert(result.dept.deptName);
			invio.set("deptUid", result.deptUid);
			invio.set("dept.deptName", result.dept.deptName);
			invio.set("ioEmployeeUid", result.employeeUid);
			invio.set("ioEmployee.employeeName", result.employeeName);
			invio.set("creatorUid", result.employeeUid);
			invio.set("creator.employeeName", result.employeeName);
			invio.set("createTime", date);
			invio.set("warehouseUid", warehouseId);
		}else {
			dorado.MessageBox.alert("请输入正确的编号...");
		}
	});
}

// @Bind #deptTree.onDataRowClick
!function(self, arg, dsInvIO) {
	var curEntity = self.get("currentEntity");
	var textEmp = view.id("textEmployee");
	if (curEntity.dataType.get("name") == "dtEmployee") {
		textEmp.set("text", curEntity.get("cardId"));
	}

	dorado.widget.DropDown.findDropDown(self).close();
}

// @Bind #dpIoType.beforeExecute
!function(self, arg, dsTypesData) {
	dsTypesData.set("parameter", {
		typeName : "iotype"
	});
	dsTypesData.flush();
}

// @Bind #dgInvInfo.onDataRowClick
!function(self, arg, dsInvIODetail, dsInvInfo) {
	var invInfo = dsInvInfo.getData("#");
	var invIODetail = dsInvIODetail.getData("#");

	invIODetail.set("partDrawingId", invInfo.get("partDrawingId"));
	invIODetail.set("batchnum", invInfo.get("batchNum"));
	invIODetail.set("sn", invInfo.get("sn"));
	invIODetail.set("warebinUid", invInfo.get("warebinUid"));
	invIODetail.set("warebin.binName", invInfo.get("warebin.binName"));
	invIODetail.set("ioQty", invInfo.get("invQty"));
	invIODetail.set("planReturnDate", new Date());
	invIODetail.set("partDescription", invInfo.get("partDescription"));
	invIODetail.set("partName", invInfo.get("partName"));
	invIODetail.set("materialUid", invInfo.get("materialUid"));

	dorado.widget.DropDown.findDropDown(self).close();

}

// @Bind @dtInvIODetail.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = dsInvIO.getData("#");
	var invIODetail = arg.entity;

	invIODetail.set("invIOUid", invIO.get("invIOId"));
}

// @Bind #warehouseTree.onDataRowClick
!function(self, arg) {
	var curEntity = self.get("currentEntity");
	warehouseId = curEntity.get("uuid");
}

// @Bind #btnQuery.onClick
!function(self, arg, afInvInfo, dsInvInfo) {
	var entity = afInvInfo.get("entity");
	entity.set("warehouseUid", warehouseId);

	dsInvInfo.set("parameter", entity).flushAsync();
}

// @Bind #dsInvInfo.onLoadData
!function(self, arg) {
	self.set("parameter", {
		wareHouseUid : warehouseId
	});
}

// @Bind #dgDetail.onContextMenu
!function(self, arg) {
	view.id("rightKeyMenu").show({
		position : {
			left : arg.event.pageX,
			top : arg.event.pageY
		}
	});

}

// @Bind #menuAdd.onClick
!function(self, dsInvIODetail) {
	var invIODetail = dsInvIODetail.insert();

}

// @Bind #manuDelete.onClick
!function(self, dsInvIODetail) {
	var invIODetail = dsInvIODetail.getData();

	if (invIODetail.toArray().length > 0) {
		invIODetail.remove();
	} else {
		dorado.MessageBox.alert("没有可删除的数据...");
	}
}

// @Bind #dgDetail.beforeCellValueEdit
!function(self, arg, dsInvInfo) {
	if (arg.column.get("name") == "ioQty"){
		var invInfo = dsInvInfo.getData("#");
		var oldQty = invInfo.get("invQty");
		var newQty = arg.value;
		if (oldQty < newQty){
			dorado.MessageBox.alert("库存数量不足，请重新输入...");
			self.get("currentEntity").set("ioQty", oldQty);
		}
	}
}

